﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using orienta.Models;

namespace orienta.Controllers
{ 
    public class CompendioElementoController : Controller
    {
        private GoodTireEntities db = new GoodTireEntities();

        //
        // GET: /CompendioElemento/

        public ViewResult  List(decimal id)
        {
            //var ORIDetCompendio1 = db.ORIDetCompendio.Include("ORIMaeCompendio").Include("ORIMaeUsuario");
            ORIMaeCompendio oRIMaeCompendio = db.ORIMaeCompendio.Single(o => o.nIdeCompendio == id);
            //Response.Write();
            ViewBag.compendioPadre = oRIMaeCompendio.vNomTabla;
            var detalleList = from m in db.ORIDetCompendio
                              where m.nIdeCompendio==id
                              select m;
            return View(detalleList.ToList());
        }

        //
        // GET: /CompendioElemento/Details/5

        public ViewResult Details(decimal id)
        {
            ORIDetCompendio ORIDetCompendio = db.ORIDetCompendio.Single(o => o.nIdeDetalle == id);
            return View(ORIDetCompendio);
        }

        //
        // GET: /CompendioElemento/Create

        public ActionResult Create()
        {
            ViewBag.nIdeCompendio = new SelectList(db.ORIMaeCompendio, "nIdeCompendio", "vNomTabla");
            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdeUsu", "vCodUsuario");
            return View();
        } 

        //
        // POST: /CompendioElemento/Create

        [HttpPost]
        public ActionResult Create(ORIDetCompendio ORIDetCompendio)
        {
            if (ModelState.IsValid)
            {
                db.ORIDetCompendio.AddObject(ORIDetCompendio);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.nIdeCompendio = new SelectList(db.ORIMaeCompendio, "nIdeCompendio", "vNomTabla", ORIDetCompendio.nIdeCompendio);
            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario", ORIDetCompendio.nIdeUsuReg);
            return View(ORIDetCompendio);
        }
        
        //
        // GET: /CompendioElemento/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            //ORIMaeCompendio oRIMaeCompendio = db.ORIMaeCompendio.Single(o => o.nIdeCompendio == id);
            //Response.Write(oRIMaeCompendio.nIdeCompendio);
            

            //ViewBag.WhichLink = "@Html.ActionLink('Edit', 'Edit', new { id = oRIMaeCompendio.nIdeCompendio })";
            ORIDetCompendio ORIDetCompendio = db.ORIDetCompendio.Single(o => o.nIdeDetalle == id);
            ViewBag.nIdeCompendioPadre = ORIDetCompendio.nIdeCompendio;
            ViewBag.nIdeCompendio = new SelectList(db.ORIMaeCompendio, "nIdeCompendio", "vNomTabla", ORIDetCompendio.nIdeCompendio);
            //ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario", ORIDetcompendio.nIdeUsuReg);
            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdeUsu", "vCodUsuario", ORIDetCompendio.nIdeUsuReg);
            return View(ORIDetCompendio);
        }

        //
        // POST: /CompendioElemento/Edit/5

        [HttpPost]
        public ActionResult Edit(ORIDetCompendio ORIDetCompendio)
        {
            if (ModelState.IsValid)
            {
                db.ORIDetCompendio.Attach(ORIDetCompendio);
                db.ObjectStateManager.ChangeObjectState(ORIDetCompendio, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.nIdeCompendio = new SelectList(db.ORIMaeCompendio, "nIdeCompendio", "vNomTabla", ORIDetCompendio.nIdeCompendio);
            //ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario", ORIDetcompendio.nIdeUsuReg);
            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario", ORIDetCompendio.nIdeUsuReg);
            return View(ORIDetCompendio);
        }

        //
        // GET: /CompendioElemento/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            ORIDetCompendio ORIDetCompendio = db.ORIDetCompendio.Single(o => o.nIdeDetalle == id);
            return View(ORIDetCompendio);
        }

        //
        // POST: /CompendioElemento/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            ORIDetCompendio ORIDetCompendio = db.ORIDetCompendio.Single(o => o.nIdeDetalle == id);
            db.ORIDetCompendio.DeleteObject(ORIDetCompendio);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}